package com.nuaa.ange.utils;

import com.alibaba.fastjson.JSONObject;
import com.nuaa.ange.dto.ArticleEs;
import com.nuaa.ange.entity.*;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @description
 * @author 玦离
 * @date 2023/1/1 19:16
 */

public class ElasticSearchUtil {

    public static RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(
                RestClient.builder(
                        //若有多个，可以传一个数组
                        new HttpHost("127.0.0.1", 9200, "http")));
    }

    static RestHighLevelClient client = restHighLevelClient();

    /**
     * 查询用的方法
     * @param indexName
     * @param queryBuilder
     * @return
     * @throws IOException
     */
    public static List<SearchHit> query(String indexName, QueryBuilder queryBuilder) throws IOException {
        //1. 创建request
        SearchRequest request = new SearchRequest(indexName);
        //2. 封装查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(queryBuilder);
        request.source(builder);
        //3. 执行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. 输出结果
        return Arrays.asList(response.getHits().getHits());
    }


    /**
     * 创建文档
     * @param article
     */
    public static void createDocument(Article article) {
        ArticleEs articleEs = new ArticleEs(article.getArticleId(), article.getTags(), article.getTitle(), article.getContent());
        //1、构建请求
        IndexRequest request = new IndexRequest("article_index");
        //2、设置规则  PUT /user_index/user/_doc/1
        //设置id
        request.id(String.valueOf(article.getArticleId()));
        //设置超时时间
        request.timeout(TimeValue.timeValueSeconds(1));
        //3、将数据放入到请求中,以JSON的格式存放
        request.source(JSONObject.toJSONString(articleEs), XContentType.JSON);
        //4、客户端发送请求,获取响应结果
        IndexResponse response = null;
        try {
            response = client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 删除文档
     * @param articleId
     * @throws IOException
     */
    public static void deleteDocument(int articleId) throws IOException {
        //构建删除请求
        DeleteRequest request = new DeleteRequest("article_index", String.valueOf(articleId));
        //客户端执行删除请求，并获取响应结果
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
    }


    /**
     * 更新文档
     * @param article
     * @throws IOException
     */
    public static void updateDocument(Article article) throws IOException {
        //更新id为1的文档的信息
        UpdateRequest request = new UpdateRequest("article_index", String.valueOf(article.getArticleId()));
        ArticleEs articleEs = new ArticleEs(article.getArticleId(), article.getTags(), article.getTitle(), article.getContent());
        request.doc(JSONObject.toJSONString(articleEs), XContentType.JSON);
        //客户端执行更新请求
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    }
}
